<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>分类管理</title>
    <link rel="stylesheet" href="/baseAdmin/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <table id="power-table" lay-filter="power-table"></table>
    </div>
</div>

<div class="layui-form" id="category" lay-filter="category" style="display: none">
    <hr class="layui-bg-gray">
    <div class="layui-form-item" style="display: none">
        <label class="layui-form-label" style="align-content: center">id</label>
        <div class="layui-inline">
            <input type="text" name="id" class="layui-input" readonly >
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label" style="align-content: center">父目录</label>
        <div class="layui-input-block">
            <select name="pid" id="pid" lay-verify="required">
                <option value="0">根目录</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label" style="align-content: center">分类名称</label>
        <div class="layui-inline">
            <input type="text" name="categoryName" lay-verify="required" required
                   placeholder="请输入分类名称" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label" style="align-content: center">分类描述</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入描述信息" style="width: 90%"
                      name="categoryDescribe" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item" align="right" style="margin-right: 10%">
        <button type="button" class="layui-btn layui-btn-danger layui-btn-radius"
                lay-submit="" lay-filter="confirm">确认</button>
        <button type="button" class="layui-btn layui-btn-radius" onclick="cancel()">取消</button>
    </div>
</div>

<script type="text/html" id="power-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
	<button class="pear-btn pear-btn-success pear-btn-md" lay-event="expandAll">
	    <i class="layui-icon layui-icon-spread-left"></i>
	    展开
	</button>
	<button class="pear-btn pear-btn-success pear-btn-md" lay-event="foldAll">
	    <i class="layui-icon layui-icon-shrink-right"></i>
	    折叠
	</button>
</script>

<script type="text/html" id="power-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
</script>

<script type="text/html" id="power-type">
    {{#if (d.powerType == '0') { }}
    <span>目录</span>
    {{# }else if(d.powerType == '1'){ }}
    <span>菜单</span>
    {{# }else if(d.powerType == '2'){ }}
    <span>按钮</span>
    {{# } }}
</script>

<script type="text/html" id="power-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="user-enable" checked = "{{ d.id == 10003 ? 'true' : 'false' }}">
</script>

<script type="text/html" id="icon">
    <i class="layui-icon {{d.icon}}"></i>
</script>

<script src="/baseAdmin/layui/layui.js"></script>
<script src="/baseAdmin/pear/pear.js"></script>
<script src="/plugins/jquery/jquery-3.4.1.min.js"></script>
<script>
    layui.use(['table','form','common','jquery','treetable'],function () {
        let table = layui.table;
        let form = layui.form;
        let layer = layui.layer;
        let common = layui.common;
        let $ = layui.jquery;
        let treetable = layui.treetable;
        getCategories();

        window.render = function(){
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'powerId',
                treePidName: 'parentId',
                skin:'line',
                method:'post',
                treeDefaultClose: true,
				response: {
					statusName: 'code', //规定数据状态的字段名称，默认：code
					statusCode: 200     //规定成功的状态码，默认：0
				},
				toolbar:'#power-toolbar',
                elem: '#power-table',
                //url: '/manage/admin/data/power.json',
                url: '/system/category/list',
                page: false,
                cols: [
                    [
                    {type: 'checkbox'},
                    {field: 'id', width: 80, title: 'ID', sort: true, hide: true},
					{field: 'name', width: 200, title: '分类名称'},
					{field: 'articleNum', width: 150, align: 'center', title: '文章数'},
					{field: 'describe', minWidth: 200, title: '分类描述'},
					{templet: '#power-bar', width: 200, align: 'center', title: '操作'}
                    ]
                ]
            });
        }
        render();

        //表格操作
        table.on('tool(power-table)',function(obj){
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        })
        table.on('toolbar(power-table)', function(obj){
            if(obj.event === 'add'){
                window.add();
            } else if(obj.event === 'refresh'){
                window.refresh();
            } else if(obj.event === 'batchRemove'){
                window.batchRemove(obj);
            }  else if(obj.event === 'expandAll'){
				 treetable.expandAll("#power-table");
			} else if(obj.event === 'foldAll'){
				 treetable.foldAll("#power-table");
			}
        });

        //打开添加窗口
        window.add = function(){
            //弹出添加窗口时清空表单数据
            form.val("category", {
                "id":null,
                "categoryName":null,
                "categoryDescribe":null
            });
            layer.open({
                type: 1,
                content: $('#category'), //这里content是一个DOM
                title: "新增分类",
                area: ['500px', '400px']
            });
        }
        //打开编辑窗口
        window.edit = function(obj){
            $.ajax({
                url:"/system/category/getCategoryById?id="+obj.data.id , // 请求路径
                type:"get" , //请求方式
                success:function (res) {
                    if (res.code == 200) {
                        form.val("category", {
                            "id":res.data.id,
                            "categoryName":res.data.categoryName,
                            "categoryDescribe":res.data.categoryDescribe
                        });
                    } else {
                        layer.msg(res.message);
                    }
                },//响应成功后的回调函数
                error:function () {
                    alert("出错啦...")
                },//表示如果请求响应出现错误，会执行的回调函数
            });
            layer.open({
                type: 1,
                content: $('#category'), //这里content是一个DOM
                title: "修改分类",
                area: ['500px', '400px']
            });
        }
        //删除操作
        window.remove = function(obj){
            layer.confirm('确定要删除该分类', {icon: 3, title:'提示'}, function(index){
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url:"/system/category/deleteCategoryByIds?ids="+obj.data.id , // 请求路径
                    type:"get" , //请求方式
                    success:function (res) {
                        layer.close(loading);
                        if (res.code == 200) {
                            render();
                        } else {
                            layer.msg(res.message);
                        }
                    },//响应成功后的回调函数
                    error:function () {
                        alert("出错啦...")
                    },//表示如果请求响应出现错误，会执行的回调函数
                });
            });
        }
        //批量删除
        window.batchRemove = function(obj){
            var checkIds = common.checkField(obj,'id');
            layer.confirm('确定要删除这些分类吗，该操作不可逆', {icon: 3, title:'提示'}, function(index){
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url:"/system/category/deleteCategoryByIds?ids="+checkIds , // 请求路径
                    type:"get" , //请求方式
                    success:function (res) {
                        layer.close(loading);
                        if (res.code == 200) {
                            render();
                        } else {
                            layer.msg(res.message);
                        }
                    },//响应成功后的回调函数
                    error:function () {
                        alert("出错啦...")
                    },//表示如果请求响应出现错误，会执行的回调函数
                });
            });
        }
        //表格刷新
        window.refresh = function() {
            table.reload('power-table');
        }
        //确认
        form.on("submit(confirm)", function (data){
            $.ajax({
                url:"/system/category/addOrUpdateCategory" , // 请求路径
                type:"post" , //请求方式
                data:JSON.stringify(data.field),
                contentType:"application/json",
                dataType:"json",//设置接受到的响应数据的格式
                success:function (res) {
                    if (res.code == 200) {
                        layer.msg(res.message);
                        layer.closeAll();
                        render();
                    } else {
                        layer.msg(res.message);
                    }
                },//响应成功后的回调函数
                error:function () {
                    alert("出错啦...")
                },//表示如果请求响应出现错误，会执行的回调函数
            });
        });
        //回显弹窗分类信息
        function getCategories(){

            $.ajax({
                url:"/system/article/getCategories" , // 请求路径
                type:"get" , //请求方式
                success:function (res) {
                    if (res.code == 200) {
                        for (var i=0;i<res.data.length;i++){
                            $("#pid").append("<option value='"+res.data[i].id+"'>"+res.data[i].categoryName+"</option>");
                        }
                    }
                },//响应成功后的回调函数
                error:function () {
                    alert("出错啦...")
                },//表示如果请求响应出现错误，会执行的回调函数
            });
        }
    })

    function cancel(){
        this.layui.layer.closeAll();
    }
</script>
</body>
</html>